<?php
// Länkar till en fil 
require_once ('LoginController.php');
require_once ('./View/PageView.php');
require_once ('RegisterController.php');
require_once ('./common/DBConfig.php');
require_once ('./common/Database.php');
require_once ('./View/NavigationView.php');
require_once ('./View/BloggView.php');
require_once ('./Model/Blogg.php');
require_once ('./Model/BloggList.php');
require_once ('./Controller/PostController.php');
require_once ('./Controller/PostListController.php');
require_once ('./Controller/NewPostController.php');
// skaffar en instace av en klass från den inlänkade filen
// hämtar sedan en funktion från klassen 

//TODO: Strängeroenden FFS!
//TODO: Skapa databas, connecta, skicka in där den behövs, stäng databas.
class MasterController {
        
    public static function doControll() {
        $html ="";
        //Anslut till databasen.
        $database = new Database();
		$database->Connect(new DBConfig());
        $loginHandler = new loginHandler($database);
        $loginController = new loginController();
        $register = new RegisterController();

        //Kontrollera inloggning mha controllern
        $loginController = new LoginController();
		$nawView = new NavigationView();
		if ($nawView->isViewingPost())
		{
            $bloggList = new bloggList();
			$PostControll = new PostController();
			$pco = $PostControll->DoControll($database, $bloggList);
			$html = $pco->m_BloggHtml;
		}
		else {
			$PostListController = new PostListController();
			$html = $PostListController->DoControll();
		}
        
      	// gör aå att du kan komma till registreringen
        if(isset($_GET['page']))
        {
            if ($_GET['page'] == 'register'){
                $html = $register->DoControll($database);
            }
			else if ($_GET['page'] == 'login') {
            $html = $loginController->DoControll($loginHandler);
			}
            else if ($_GET['page'] == 'newpost')
            {     
                $nwp = new NewPostController();
                $html = $nwp->DoControll($database);
            }
        }
        // Skickar vidare den till pageview
        $pageView = new PageView();
        return $pageView->GetHTMLPage("Wibjers", $html);
		$database->Close($database);		
        }
        
}

?>